<?php
if (isset($_POST['pin']) && isset($_POST['xing'])){
    include '../c/config.php';
    require_once "../jssdk.php";
    $pin=addslashes(trim($_POST['pin']));
    $xing=addslashes(trim($_POST['xing']));
    $dian=addslashes(trim($_POST['dian']));
    $dui=addslashes(trim($_POST['dui']));
    $xiao=addslashes(trim($_POST['xiao']));
    $mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
    if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqliTwo->error);}
    
    
    $upDui=strtoupper($dui);
    $query=$mysqli->query("select id from ucard where code='".$upDui."' limit 0,1 ");
    if($query->num_rows>0){
        $getUpInfo=$query->fetch_array();
        $upId=$getUpInfo['id'];
         
    }
    
    
    
    $query=$mysqli->query("select id from xma where name='".strtoupper($xiao)."' limit 0,1 ");
    if($query->num_rows>0){
        
    }else{
        echo json_encode(array('status'=>'not', 'msg'=>'nxma'));
        exit;
    }
    
    
    function send_post($url, $post_data) {
        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-type:application/json;charset=UTF-8',
                'content' => $post_data,
                'timeout' => 15 * 60 // 超时时间（单位:s）
            )
        );
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        
        return $result;
    }
    
    $jssdk = new JSSDK($appId, $secret);
    $signPackage = $jssdk->GetSignPackage();
    $acToken=$jssdk->getAccessToken();
    $url='https://api.weixin.qq.com/card/code/get?access_token='.$acToken;
    $cardInfo='{
       "card_id" : "'.$cardId.'",
       "code" : "'.$dui.'",
       "check_consume" : true
    }';
    $checkdata=json_decode(send_post($url, $cardInfo),true);

    if(40056 == $checkdata['errcode']){
         
       // echo json_encode(array('status'=>'not', 'msg'=>'buzai'));
       // exit;
    }
 
    $can_consume=true;
    if (isset($checkdata['can_consume'])){
        $can_consume=$checkdata['can_consume'];
    }
    $errmsg='';
    if (true == $can_consume){
        $url='https://api.weixin.qq.com/card/code/consume?access_token='.$acToken;
        $cardInfo='{
           "card_id" : "'.$cardId.'",
           "code" : "'.$dui.'"
        }';
        $data=json_decode(send_post($url, $cardInfo),true);
        $errmsg=$data['errmsg'];
    }
 
    
    $query=$mysqli->query("select id from xiao where dui='".$dui."'  limit 0,1 ");
    if($query->num_rows>0){
       echo json_encode(array('status'=>'not', 'msg'=>'has'));
       exit;    
    }
     
    $mysqli->query("insert into xiao(pin,xing,dian,dui,xiao,msg,createtime) values('".$pin."','".$xing."','".$dian."','".$dui."',
'".$xiao."','".$errmsg."','".date("Y-m-d H:i:s")."')  ");
    echo json_encode(array('status'=>'work', 'msg'=>'work'));
     
}else{
    echo json_encode(array('status'=>'not', 'msg'=>'has'));
    exit;
}

